package com.mby.controller;

import com.mby.entity.ChatGroup;
import com.mby.entity.DeviceToken;
import com.mby.entity.Push;
import com.mby.service.IPushService;
import com.mby.service.IRongYunService;
import com.mby.utils.CsAppUtil;
import com.mby.utils.MbyPush;
import com.mby.utils.WebConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by developer_01 on 2018/6/11.
 */

@RestController
@RequestMapping("/rabbit")
public class RabbitController extends BaseController{

    @Resource
    private IPushService pushService;

    @Resource
    private IRongYunService rongYunService;

    @GetMapping(value = "/est15")
    public void pushEst15() throws Exception{
        List<?> list = mongoTool.findMany("{'est15':0,'estHandoverAt!':null,'deletedAt':null}","", ChatGroup.class);
        if(list.size() > 0){
            ChatGroup chatGroup; long seconds;
            for(int i=0;i<list.size();i++){
                chatGroup = (ChatGroup) list.get(i);
                seconds = getSeconds(chatGroup.getEstHandoverDate(),new Date());
                if(seconds <= 900){
                    pushService.sentEst15Push(chatGroup);
                    mongoTool.update("{'est15':1}","{'id':"+chatGroup.getId()+"}",ChatGroup.class);
                }
            }
        }
        respData(ok);
    }

    @GetMapping(value = "/checked40")
    public void checked40() throws Exception{
        List<?> list = mongoTool.findMany("{'checked40':0,'qualityCheckAt!':null,'deletedAt':null}","", ChatGroup.class);
        if(list.size() > 0){
            ChatGroup chatGroup; long seconds;
            for(int i=0;i<list.size();i++){
                chatGroup = (ChatGroup) list.get(i);
                seconds = getSeconds(new Date(),chatGroup.getQualityCheckDate());
                if(seconds >= 2400){
                    pushService.sentStatusPush(chatGroup);
                    rongYunService.sentStatusMsg(chatGroup,4);
                    mongoTool.update("{'checked40':1}","{'id':"+chatGroup.getId()+"}",ChatGroup.class);
                }
            }
        }
        respData(ok);
    }

}
